var typeMap={"primeTypeservice":"一次设备超期服役","primeTypecheck":"一次设备超期未检","primeTypecounter":"一次设备反措未执行",
		"primeTypedefect":"一次设备未处理缺陷","primeTypectsa":"CT饱和","primeTypebreakerExp":"断路器动作异常",
		"secondTypeservice":"二次设备超期服役","secondTypecheck":"二次设备超期未检","secondTypecounter":"二次设备反措未执行",
		"secondTypedefect":"二次设备未处理缺陷","secondTypechannel":"通信中断","secondTypedevice":"保护装置异常","secondTyperesponse":"保护响应异常"};
var breakExpParam;
var tb=null;
function getData(){
	if(type.indexOf("primeType")>-1){
		//属于一次设备
		$.getJSON("/rest/primerisk/getPrimeInfoByType/"+type+"/"+stationId,function(result){
			breakExpParam=result.param;
			$(".title").html(typeMap[type]);
			//initTbData(result.info);
			initMainPie(result);
			initSubPie(result);
		})
	}else{
		//属于二次设备
		$.getJSON("/rest/SecRiskEvaluateService/getSecondInfoByType/"+type+"/"+stationId,function(result){
			$(".title").html(typeMap[type]);
			//initTbData(result.info);
			initMainPie(result);
			initSubPie(result);
		})
	}
}
function backToPie(){
	$("#tbDiv").hide();
	$("#pieDiv").show();
}
//左边饼图是发生异常的设备数目占总体比例
function initMainPie(dataObj){
	var expDeviceNum=0;
	for(var name in dataObj.info){
		expDeviceNum++;
	}
	var pieData=[["正常",dataObj.count-expDeviceNum]];
	pieData.push(["异常设备",expDeviceNum]);
	var mainPie=new Pie("mainPie");
	mainPie.setColors(["#00a65a","#dd4b39"]);
	mainPie.setPie(pieData);
	var option = mainPie.getOption();
	option.title.text="异常设备数目占比";
	option.legend.top=30;
	//option.legend.orient='vertical';
	//option.legend.x='left';
	option.tooltip.formatter= "{b}: {c} ({d}%)";
	option.series[0].label.normal.formatter="{b}: {c} ({d}%)";
	mainPie.setPieOuterRadius("60%");
	mainPie.show();
}
//右边饼图是各个异常设备发生的次数
function initSubPie(dataObj){
	var pieData=[];
	for(var name in dataObj.info){
		pieData.push([name,dataObj.info[name].length]);
	}
	var subPie=new Pie("subPie");
	subPie.setPie(pieData);
	var option = subPie.getOption();
	option.title.text = "各异常设备发生异常次数分布";
	option.legend.top=40;
	option.legend.orient='vertical';
	option.legend.x='left';
	subPie.setPieOuterRadius("60%");
	option.tooltip.formatter= "{b}: {c} ({d}%)";
	option.series[0].label.normal.formatter="{b}: {c} ({d}%)";
	subPie.show();
	if(type!="secondTypedevice"){
		//保护装置异常饼图不可点
		subPie.getChart().on("click",function(params){
			//展示详细列表
			if(tb){
				tb.getTable().destroy();
				$("#infoTb").empty();
			}
			$("#tbDiv").show();
			$("#pieDiv").hide();
			var name=params.name;
			$("#tbTitle span").html(name);
			initTbData(dataObj.info[name]);
		});
	}
}
function getLocalTime(nS) {
	return new Date(nS).toLocaleString();
}
function initTbData(value) {
	var info = [];
	var evalType=type.split("Type")[1];
	switch (evalType) {
	case "counter":
		// 反措未执行
		for ( var i in value) {
			info.push([ value[i].deviceName, value[i].content,
					value[i].responsibleDepartment,
					getLocalTime(value[i].planStartDate.time),
					getLocalTime(value[i].planFinishDate.time) ]);
		}
		var columns = [ "设备名称", "反措内容", "责任单位", "计划时间", "计划结束时间" ];
		initTb(columns, info);
		break;
	case "defect":
		// 缺陷
		for ( var i in value) {
			info.push([ value[i].deviceName, value[i].defectDesc,
					value[i].manufacturer, value[i].deviceType ]);
		}
		var columns = [ "设备名称", "缺陷类型", "生产厂家", "设备类型" ];
		initTb(columns, info);
		break;
	case "service":
		// 超期服役
		for ( var i in value) {
			info.push([ value[i].name, getLocalTime(value[i].runDate.time),
					value[i].runLimit + "天", value[i].manufacturer,
					value[i].voltage ]);
		}
		var columns = [ "设备名称", "投运日期", "运行年限", "生产厂家", "电压等级" ];
		initTb(columns, info);
		break;
	case "check":
		// 超期未检
		for ( var i in value) {
			info.push([ value[i].deviceName,
					getLocalTime(value[i].planFinishDate.time),
					getLocalTime(value[i].planStartDate.time),
					value[i].responsibleDepartment ]);
		}
		var columns = [ "设备名称", "计划完成日期", "计划开始日期", "责任单位" ];
		initTb(columns, info);
		break;
	case "breakerExp":
		// 断路器隐患
		for ( var i in value) {
			info.push([ value[i].breakerName,value[i].faultTime,
			           Number(value[i].distance0MomentsTime)/1000, Number(value[i].xhTimeInterval)/1000,
			           Number(value[i].cross0Time)/1000,value[i].phase,
			           "<button class='btn btn-primary' onclick='openReport(\""+value[i].lineId+"\",\""+value[i].faultTime+"\",\""+value[i].lineName+"\")'><i class='fa fa-search'></i></button>"]);
		}
		var columns = [ "开关名称","故障时间", "动作时间(ms)","熄弧时间(ms)","过零点时间(ms)", "相别",""];
		initTb(columns, info);
		break;
	case "ctsa":
		// CT饱和   TODO 故障点时间  最大电流时间
		for ( var i in value) {
			info.push([ value[i].lineName,value[i].faultTime,value[i].phase,
			            Math.abs(value[i].maxCurrent).toFixed(2), 
			            value[i].seventhPowerHarmonicProportion==""?"":Number(value[i].seventhPowerHarmonicProportion).toFixed(2),
			            value[i].areaProportion==""?"":Number(value[i].areaProportion).toFixed(2),
			            "<button class='btn btn-primary' onclick='openReport(\""+value[i].lineId+"\",\""+value[i].faultTime+"\",\""+value[i].lineName+"\")'><i class='fa fa-search'></i></button>" ]);
		}
		var columns = [ "线路","故障时间", "相别", "最大电流","谐波比","面积比",""];
		initTb(columns, info);
		break;
	case "channel":
		// 通信中断
		for ( var i in value) {
			info.push([ getLocalTime(value[i].eventStart.time),value[i].alarmMsg,
					value[i].durationTime,value[i].revertTime==null?"未复归":getLocalTime(value[i].revertTime.time),value[i].voltage
					]);
		}
		var columns = ["告警时间", "告警内容", "持续时间(ms)", "复归时间","电压等级"];
		initTb(columns, info);
		break;
	case "device":
		// 保护装置异常
		for ( var i in value) {
			info.push([ value[i].protectName,value[i].continueMs,value[i].voltage]);
		}
		var columns = [ "保护名称","持续时间(ms)","电压等级"];
		initTb(columns, info);
		break;
	case "response":
		// 保护响应异常
		for ( var i in value) {
			info.push([ value[i].protectionName,
					value[i].faultTime,Number(value[i].distance0MomentsTime)/1000,
					value[i].phase,value[i].manufacturer,
					 "<button class='btn btn-primary' onclick='openReport(\""+value[i].lineId+"\",\""+value[i].faultTime+"\",\""+value[i].lineName+"\")'><i class='fa fa-search'></i></button>"]);
		}
		var columns = [ "保护名称", "故障时间", "动作时间(ms)","故障相别","厂家",""];
		initTb(columns, info);
		break;
	}
}
//链接到故障报告(lineId,faultTime,lineName)
function openReport(lineId,faultTime,lineName){
	if(lineId!=""&&faultTime!=""){
		openFaultReportPage(lineId,faultTime,lineName);
	}
}
function initTb(columns, info) {
	var opt = MyTable.getDefaultOptions();
	opt.searching=true;
	opt.paging=true;
	opt.lengthChange=true;
	tb = new MyTable('infoTb', columns, opt);
	tb.setDataSet(info);
	tb.show();
	if(type.indexOf("breaker")>-1){
		//断路器动作异常的要标出是动作时间异常还是熄弧时间异常还是过零点异常
		tb.getTable().cells().every(function() {
			var data = this.data();
			if ((Number(data) >=Number(breakExpParam["act"])/1000 && this[0][0].column ==2)
					||(Number(data) >Number(breakExpParam["xh"])/1000 && this[0][0].column ==3)
					||(Number(data) >Number(breakExpParam["cross0"])/1000 && this[0][0].column == 4)) {
				$(this.node()).css("color","red");
			}
		});
	}
}
